package com.elibrary;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import nimo.elibrary.json.model.book.Book;
import nimo.elibrary.json.model.book.InPress;

/**
 * Created with IntelliJ IDEA.
 * User: Enkuso
 * Date: 1/28/13
 * Time: 10:14 AM
 * To change this template use File | Settings | File Templates.
 */
public class BookDetailFragment extends CountingFragment {
    public TextView bookTitle;
    public TextView bookAuthor;
    public TextView bookIntroduction;
    public ImageView bookCover;
    public TextView bookTags;
    public TextView bookCategoryAndPublishedYear;
    public TextView bookPageCount;
    public TextView bookRating;
    public LinearLayout bookReviews;
    public LinearLayout bookInPressContainer;
    public LinearLayout relatedBooks;
    public Button btnMoreReviews;
    Book book;

    Context context;
    View v;
    LayoutInflater inflater;

    public BookDetailFragment() {

    }

    public BookDetailFragment(int num, Book book) {
        super(num);
        context = BookstoreApplication.getAppContext();
        this.book = book;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.book_detail, container, false);
        this.inflater = inflater;

        // get text views
        bookTitle = (TextView) v.findViewById(R.id.tvBookTitle);
        bookAuthor = (TextView) v.findViewById(R.id.tvBookAuthor);
        bookIntroduction = (TextView) v.findViewById(R.id.tvBookInformation);
        bookCover = (ImageView) v.findViewById(R.id.imgBookCover);
        bookTags = (TextView) v.findViewById(R.id.tvBookCategoryAndPublishedYear);
        bookCategoryAndPublishedYear = (TextView) v.findViewById(R.id.tvBookCategoryAndPublishedYear);
        bookPageCount = (TextView) v.findViewById(R.id.tvBookPageCount);
        bookRating = (TextView) v.findViewById(R.id.tvBookRating);
        bookReviews = (LinearLayout) v.findViewById(R.id.reviews);
        bookInPressContainer = (LinearLayout) v.findViewById(R.id.inPressContainer);
        relatedBooks = (LinearLayout) v.findViewById(R.id.relatedBooksContainer);

        btnMoreReviews = (Button) v.findViewById(R.id.btnLoadMoreReviews);

        setBook();

        return v;
    }

    @Override
    public void onResume() {
        super.onResume();
        ((BaseActivity)getSherlockActivity()).currentFragment = Constants.FRAGMENT_BOOKDETAIL;
    }

    private void setBook(){

        bookTitle.setText(book.title);
        bookAuthor.setText(book.authorName);
        bookIntroduction.setText(book.introduction);
        bookPageCount.setText(String.valueOf(book.pageCount) + " " + getString(R.string.page));

        // cover zurag
        ((BookstoreApplication)context.getApplicationContext()).imageLoader.DisplayImage(book.cover, bookCover);

        // tags
        String tag = "";
        int tag_len = book.tags.length - 1;
        for (int i=0; i<=tag_len; i++){
            tag += book.tags[i];
            if(i != tag_len)
                tag += ", ";
        }
        bookTags.setText(tag);

        // rating
        bookRating.setText(book.rating + " (" + book.ratesCount + " " + getString(R.string.rates) + ")");
        int rate = (int)book.rating;
        int[] star = {R.drawable.star0, R.drawable.star1, R.drawable.star2, R.drawable.star3, R.drawable.star4, R.drawable.star5};
        bookRating.setCompoundDrawablesWithIntrinsicBounds(star[rate], 0, 0, 0);

        // reviews
        bookReviews.removeAllViews();
        int count = book.rates.size();
        for(int i=0;i<count;i++){
            bookReviews.addView(inflater.inflate(R.layout.review, null), i);
            ViewGroup review = (ViewGroup) bookReviews.getChildAt(i);
            ViewGroup reviewer = (ViewGroup) review.getChildAt(0);
            TextView name = (TextView) reviewer.getChildAt(0);
            TextView score = (TextView) reviewer.getChildAt(1);
            TextView comment = (TextView) review.getChildAt(1);
            comment.setText(book.rates.get(i).comment);
            name.setText(book.rates.get(i).reviewerName);
            score.setCompoundDrawablesWithIntrinsicBounds(0,0,star[book.rates.get(i).rate],0);
        }
        if(count == 0){
            // Сэтгэгдэл байхгүй
            TextView nocomment = new TextView(context);
            nocomment.setText(getString(R.string.nocomment));
            bookReviews.addView(nocomment);
        }
        // Configuring load more comments button
        if(count < book.ratesCount){
            btnMoreReviews.setVisibility(View.VISIBLE);
        } else {
            btnMoreReviews.setVisibility(View.INVISIBLE);
        }

        // Ойролцоо бүтээлүүд
        if(book.related.size() > 0) {
            LinearLayout relatedBooks = (LinearLayout) v.findViewById(R.id.relatedBooksCoverContainer);
            relatedBooks.removeAllViews();
            LinearLayout.LayoutParams layout_params = new LinearLayout.LayoutParams(100,150);
            layout_params.setMargins(0, 0, 10, 0);
            for(final Book relatedBook : book.related) {
                //ImageView imageView = (ImageView) getLayoutInflater().inflate(R.layout.related_book_cover, null);
                ImageButton imageButton = new ImageButton(context);
                imageButton.setBackgroundResource(R.drawable.default_cover);
                imageButton.setLayoutParams(layout_params);
                imageButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        ((BaseActivity)getSherlockActivity()).gotoBookDetail(view,relatedBook.id);
                        //Intent bookDetail = new Intent("com.elibrary.BOOKDETAIL");
                        //bookDetail.putExtra("book_id", relatedBook.id);
                        //startActivity(bookDetail);
                    }
                });
                ((BookstoreApplication)context.getApplicationContext()).imageLoader.DisplayImage(relatedBook.cover, imageButton);
                relatedBooks.addView(imageButton);
            }
        }
        else {
            ((LinearLayout)relatedBooks.getParent()).removeView(relatedBooks);
        }

        // Бүтээлийн талаар хэвлэл мэдээллээр
        if(book.inPress.size() > 0 ) {
            bookInPressContainer.removeAllViews();
            for(InPress inPress : book.inPress) {
                ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.hewlel_medeelel, null);
                TextView title = (TextView) viewGroup.findViewById(R.id.tvInPressTitle);
                TextView body = (TextView) viewGroup.findViewById(R.id.tvInPressBody);
                TextView source = (TextView) viewGroup.findViewById(R.id.tvInPressSource);
                title.setText(inPress.title);
                body.setText(inPress.body);
                source.setText(inPress.source);
                bookInPressContainer.addView(viewGroup);
            }
        }
        else {
            ((LinearLayout)bookInPressContainer.getParent()).removeView(bookInPressContainer);
        }

        // set currently viewing book
        ((BaseActivity)getSherlockActivity()).setCurrentlyViewingBook(book);
    }
}
